<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Term_posts_m extends Base_model {
	public $primary_key = 'term_id';
	public $_table = "term_posts";
	
	function get_the_terms($post_id = 0, $taxonomy = 'category') {
		if(!$post_id) return false;
		$this->select('term.term_id')
				->as_array();
		if($taxonomy != '')
		{
			$this->join('term','term.term_id = term_posts.term_id');
			$this->where('term_type',$taxonomy);
		}
		$result = $this->get_many_by('post_id', $post_id);
		//echo $this->db->last_query();
		return array_column($result, 'term_id');
	}

	/**
	 * Set the terms for a post.
	 *
	 *
	 * @see wp_set_object_terms()
	 *
	 * @param int    $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
	 * @param string $tags     Optional. The tags to set for the post, separated by commas. Default empty.
	 * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'.
	 * @param bool   $append   Optional. If true, don't delete existing tags, just add on. If false,
	 *                         replace the tags with the new tags. Default false.
	 * @return mixed Array of affected term IDs. False on failure.
	 */
	function wp_set_post_terms( $post_id = 0, $terms = array(), $taxonomy='category' ) {
        $post_id = (int) $post_id;

        if ( !$post_id)
            return false;

        if(!is_array($terms)) 
        	$terms = array();

        // Insert new terms for the post
        $old_term_posts = $this->get_the_terms($post_id, $taxonomy);

        if($old_term_posts)
        foreach($old_term_posts as $old_term_post){
        	if(!in_array($old_term_post, $terms))
        		$this->delete_by(array(
        			'post_id' => $post_id, 
        			'term_id' => $old_term_post
    			));
        }

        if($terms)
        foreach($terms as $term) {
        	if(!in_array($term, $old_term_posts))
        		$this->insert(array(
					'post_id' => $post_id,
					'term_id' => $term
				));
        }
		return true;
	}

	function wp_delete_term_posts($term_id=0,$posts = array(),$taxonomy = 'category'){
		$term_id = (int) $term_id;

        if ( !$term_id)
            return false;

        if($posts){
        	$this->where_in('post_id', $posts)->delete_many_by('term_id',$term_id);
        	return true;
	    }
	}

	function wp_set_term_posts( $term_id = 0, $posts = array(), $taxonomy='category' ) {
        $term_id = (int) $term_id;

        if ( !$term_id)
            return false;

        // if(!is_array($posts)) 
        // 	$terms = array();
        //  // Insert new terms for the post
        //  $old_posts_term = $this->get_the_posts($term_id, $taxonomy);
        //  if($old_posts_term)
        //  foreach($old_posts_term as $old_post_term){
        //  	if(in_array($old_post_term, $posts))
        //  		$this->delete_by(array(
        //  			'post_id' => $old_post_term, 
        //  			'term_id' => $term_id,
        // ));
        //  }
        $old_terms_post = $this->get_many_by('term_id', $term_id);
        $id_old_terms_post = array();
        if($old_terms_post)
        {
            foreach($old_terms_post as $old_term_post){
                array_push($id_old_terms_post, $old_term_post->post_id);
            }
        }
        if($posts)
        foreach($posts as $post) {
        	if(!in_array($post, $id_old_terms_post))
        		$this->insert(array(
					'post_id' => $post,
					'term_id' => $term_id
				));
        }
		return true;
	}
}

?>